package net.androy.smugmugtool;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class MainMenu extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	setContentView(R.layout.main);
	ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
		android.R.layout.simple_list_item_1, new String[] { "Search",
			"All-time Popular", "Today's Popular" }) {
	    @Override
	    public View getView(int position, View convertView, ViewGroup parent) {
		View v = super.getView(position, convertView, parent);
		((TextView) v).setTextColor(Color.parseColor("#6bca1e"));
		return v;
	    }
	};
	setListAdapter(adapter);
	getListView().setTextFilterEnabled(true);
	getListView().clearFocus();
	// getListView().setSelector(Color.parseColor("#000000"));
	getListView().setDrawSelectorOnTop(false);

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
	super.onListItemClick(l, v, position, id);

	Log.d("Clicked", position + "");

	if (position == 0) {
	    showDialog(0);
	} else if (position == 1) {
	    Intent intent = new Intent("net.androy.smugmugtool.SEARCH_VIEW");
	    intent.putExtra("mode", "ALLTIME");
	    startActivity(intent);
	} else if (position == 2) {
	    Intent intent = new Intent("net.androy.smugmugtool.SEARCH_VIEW");
	    intent.putExtra("mode", "TODAY");
	    startActivity(intent);
	}
    }

    protected Dialog onCreateDialog(int id) {
	switch (id) {
	case 0:
	    LayoutInflater factory = LayoutInflater.from(this);
	    final View textEntryView = factory.inflate(R.layout.search_layout,
		    null);
	    final EditText keyword = (EditText) textEntryView
		    .findViewById(R.id.keyword_edit);
	    return new AlertDialog.Builder(MainMenu.this)
		    // .setIcon(andrid.R.id.icon)
		    .setTitle("Search Smugmug").setView(textEntryView)
		    .setPositiveButton("Ok",
			    new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,
					int whichButton) {
				    Intent intent = new Intent(
					    "net.androy.smugmugtool.SEARCH_VIEW");
				    intent.putExtra("keyword", keyword
					    .getText().toString());
				    intent.putExtra("mode", "SEARCH");
				    startActivity(intent);
				}
			    }).setNegativeButton("Cancel",
			    new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,
					int whichButton) {

				    /* User clicked cancel so do some stuff */
				}
			    }).create();
	}
	return null;
    }
}
